-- tekDebug
local debugf = tekDebug and tekDebug:GetFrame('yChat-gfw')
local debug = function() end
if debugf then
	debug = function(...) debugf:AddMessage(string.join(', ', tostringall(...))) end
end

local FilterItems = {
	-- dungeon badge
	[29434] = true, -- Badge of Justice
	[40752] = true, -- Emblem of Heroism
	[40753] = true, -- Emblem of Valor

	-- battelground
	[20558] = true, -- Warsong Gulch Mark of Honor
	[20559] = true, -- Arathi Basin Mark of Honor
	[20560] = true, -- Alterac Valley Mark of Honor
	[29024] = true, -- Eye of the Storm Mark of Honor
	[42425] = true, -- Strand of the Ancients Mark of Honor

	-- Kael'thas Sunstrider: seven legendary weapons
	[30311] = true,
	[30312] = true,
	[30313] = true,
	[30314] = true,
	[30316] = true,
	[30317] = true,
	[30318] = true,
	[30319] = true,
}

local loc = GetLocale()
local white_list = loc == 'zhCN' and {
	'^你获得了',
	'^你得到了物品',
	'^你制造',
--[[
LOOT_ITEM_PUSHED_SELF = "你获得了物品：%s。";
LOOT_ITEM_PUSHED_SELF_MULTIPLE = "你获得了：%sx%d。";
LOOT_ITEM_SELF = "你获得了物品：%s。";
LOOT_ITEM_SELF_MULTIPLE = "你得到了物品：%sx%d。";

LOOT_ITEM_CREATED_SELF = "你制造了：%s。";
LOOT_ITEM_CREATED_SELF_MULTIPLE = "你制造了：%sx%d。";
]]
} or loc == 'zhTW' and {
	'^你獲得',
	'^你拾取了',
	'^你製造',
--[[
LOOT_ITEM_PUSHED_SELF = "你獲得了物品:%s。";
LOOT_ITEM_PUSHED_SELF_MULTIPLE = "你獲得物品:%sx%d。";
LOOT_ITEM_SELF = "你拾取了物品:%s。";
LOOT_ITEM_SELF_MULTIPLE = "你獲得戰利品:%sx%d。";

LOOT_ITEM_CREATED_SELF = "你製造了:%s。";
LOOT_ITEM_CREATED_SELF_MULTIPLE = "你製造:%sx%d。";

]]
} or { -- EN
	'^You receive',
	'^You create',
--[[
LOOT_ITEM_PUSHED_SELF = "You receive item: %s.";
LOOT_ITEM_PUSHED_SELF_MULTIPLE = "You receive item: %sx%d.";
LOOT_ITEM_SELF = "You receive loot: %s.";
LOOT_ITEM_SELF_MULTIPLE = "You receive loot: %sx%d."

LOOT_ITEM_CREATED_SELF = "You create: %s.";
LOOT_ITEM_CREATED_SELF_MULTIPLE = "You create: %sx%d.";
]]
}

ChatFrame_AddMessageEventFilter('CHAT_MSG_LOOT', function(self, event, msg, ...)
	for i = 1, #white_list do
		if strfind(msg, white_list[i]) then return end
	end
	
	local id = (type(msg) == 'string') and msg:match('\124Hitem:(%d+):')
	if id then
		debug('CHAT_MSG_LOOT', id, msg)
		if FilterItems[tonumber(id)] then return true end
	end
end)


local player, lastmsg, lastsender, lastcounter, lastFilter = UnitName'player'
ChatFrame_AddMessageEventFilter('CHAT_MSG_CHANNEL', function(self, event, msg, sender, _, _, _, _, _, ChannelNum, ChannelName, _, counter)
	if counter == lastcounter then
		return lastFilter
	end
	local filter = false
	if (msg == lastmsg) and (sender == lastsender) and (ChannelNum <= 4) and (sender ~= player) then
		filter = true
		debug('CHAT_MSG_CHANNEL', filter, counter, sender, ChannelNum, ChannelName, msg)
	end
	
	lastmsg, lastsender, lastcounter = msg, sender, counter
	lastFilter = filter
	return filter
end)

local class
if select(4,GetBuildInfo()) < 30200 then
	class = {
		['术士'] = 'WARLOCK',
		['战士'] = 'WARRIOR',
		['猎人'] = 'HUNTER',
		['法师'] = 'MAGE',
		['牧师'] = 'PRIEST',
		['德鲁伊'] = 'DRUID',
		['圣骑士'] = 'PALADIN',
		['萨满祭司'] = 'SHAMAN',
		['潜行者'] = 'ROGUE',
		['死亡骑士'] = 'DEATHKNIGHT',
	}
else
	class = {}
	for k, v in pairs(LOCALIZED_CLASS_NAMES_MALE) do
		class[v] = k
	end
	for k, v in pairs(LOCALIZED_CLASS_NAMES_FEMALE) do
		class[v] = k
	end
end

ChatFrame_AddMessageEventFilter('CHAT_MSG_WHISPER', function(self, event, msg)
	if msg:match('^<DBM>') then return true end
	
	if MiDKP_Options then
		local dkp = strlower(MiDKP_Options.whisper_command)
		msg = strlower(msg)
		
		if msg == dkp then return true end
		
		local c = msg:match('^' .. dkp .. ' (.+)')
		if c and class[c] then return true end
	end
end)
